﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace TFSAdministrationTool.Common
{
  public class TfsProxyCollection
  {
    #region Fields
    private Dictionary<string, ITeamFoundationServerProxy> m_TeamFoundationServers = new Dictionary<string, ITeamFoundationServerProxy>();
    public string SelectedServerName { get; set; }
    #endregion

    public void Add(string name, ITeamFoundationServerProxy proxy)
    {
      if (m_TeamFoundationServers.ContainsKey(name))
      {
        m_TeamFoundationServers[name] = proxy;
      }
      else
      {
        m_TeamFoundationServers.Add(name, proxy);
      }
    }

    public bool ContainsServer(string name)
    {
      return m_TeamFoundationServers.ContainsKey(name);
    }

    public ITeamFoundationServerProxy GetServerByName(string name)
    {
      return m_TeamFoundationServers[name];
    }

    public void Remove(string name)
    {
      m_TeamFoundationServers.Remove(name);
    }

    #region Properties
    public int Count
    {
      get
      {
        return m_TeamFoundationServers.Count;
      }
    }
    
    public SortedList ServerSelection
    {
      get
      {
        SortedList selection = new SortedList(m_TeamFoundationServers.Count);

        foreach (KeyValuePair<string, ITeamFoundationServerProxy> entry in m_TeamFoundationServers)
        {
          ArrayList projects = new ArrayList(entry.Value.TeamProjects.Count);

          foreach (string uri in entry.Value.TeamProjects.Values)
          {
            projects.Add(uri);
          }

          selection.Add(entry.Value.ServerUri.ToString().ToLowerInvariant(), projects);
        }

        return selection;
      }
    }
    
    public ITeamFoundationServerProxy SelectedServer
    {
      get
      {
        return m_TeamFoundationServers[SelectedServerName];
      }
    }
    #endregion
  }
}